﻿using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Windows;
using System.Windows.Controls;
using Logistics.Configuration;
using Logistics.Print;

namespace Logistics.Win
{
    /// <summary>
    /// Interaction logic for PanelPrinterSetting.xaml
    /// </summary>
    public partial class PanelPrinterSetting : UserControl
    {
        protected PanelPrinterSettingModel SettingModel { get; set; }

        private String XPath_Tags = "/system/print/tags";
        private String XPath_Ticket = "/system/print/ticket";
        private String SettingFilePath = GlobalObjects.ConfigFileLocation;

        public PanelPrinterSetting()
        {
            InitializeComponent();
            IniteSetting();
        }

        private void IniteSetting()
        {
            this.SettingModel = new PanelPrinterSettingModel();

            IList<String> PrintList = new List<String>();
            foreach (String print in PrinterSettings.InstalledPrinters)
                PrintList.Add(print);
            this.SettingModel.PrinterList = PrintList;

            String sTagPrinter = XmlFiles.Read(SettingFilePath, XPath_Tags,"");
            String sTagMaxNum = XmlFiles.Read(SettingFilePath, XPath_Tags, "maxNum");
            String sTicketPrinter = XmlFiles.Read(SettingFilePath, XPath_Ticket, "");

            this.SettingModel.TagMaxNum = String.IsNullOrEmpty(sTagMaxNum) ? 30 : Convert.ToInt32(sTagMaxNum);
            this.SettingModel.TagPrinter = sTagPrinter;
            this.SettingModel.TicketPrinter = sTicketPrinter;
            this.DataContext = SettingModel;
        }

        private void BtnSave_Click(object sender, RoutedEventArgs e)
        {
            XmlFiles.Update(SettingFilePath, XPath_Tags, "maxNum", this.SettingModel.TagMaxNum.ToString());
            XmlFiles.Update(SettingFilePath, XPath_Tags, "", this.SettingModel.TagPrinter);
            XmlFiles.Update(SettingFilePath, XPath_Ticket, "", this.SettingModel.TicketPrinter);
            Xceed.Wpf.Toolkit.MessageBox.Show("保存成功");
        }

        /// <summary>
        /// Test
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnTestTag_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                PrinterUtils.PrintWayBillTags(PrinterUtils.PRINTER_TEST_BILL_ID, 1, this.SettingModel.TagPrinter);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// Test
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnTestTicket_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                PrinterUtils.PrintWayBill(PrinterUtils.PRINTER_TEST_BILL_ID, this.SettingModel.TicketPrinter);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
